﻿using System;
using System.IO;
using Shanfree.Framework.Utility;
using BLL = Shanfree.SharpMovie.BLL;
using Shanfree.SharpMovie.Model;
using SIRC.Framework.Net;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Diagnostics;

public partial class Movie_Add : BLL.BasePage
{
    /// <summary>
    /// 影片主键
    /// </summary>
    protected string MovieID
    {
        get
        {
            return GetViewState("MovieID");
        }
        set
        {
            ViewState["MovieID"] = value;
        }
    }

    #region 事件
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                this.MovieID = GetRequest("ID");
                ShowInfo(this.MovieID);
            }
        }
        catch (Exception ex)
        {
            LogEntry.Log.Write(ex.ToString(), EventLogEntryType.Error, LogSourceType.一般错误);
            ShowMsg(ex.Message);
        }
    }

    protected void gvUri_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button btnAdd = (Button)e.Row.FindControl("btnAdd");
            Button btnEdit = (Button)e.Row.FindControl("btnEdit");
            Button btnDel = (Button)e.Row.FindControl("btnDel");
            TextBox tbName = (TextBox)e.Row.FindControl("tbName");
            TextBox tbPwd = (TextBox)e.Row.FindControl("tbPwd");
            TextBox tbTime = (TextBox)e.Row.FindControl("tbTime");

            if (tbName == null || string.IsNullOrEmpty(tbName.Text))
            {
                btnAdd.Visible = true;
                btnEdit.Visible = false;
                btnDel.Visible = false;
            }
            else
            {
                btnAdd.Visible = false;
                btnEdit.Visible = true;
                btnDel.Visible = true;
            }
        }
    }

    #endregion

    #region 按钮事件
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            string iMDB_ID = this.tbIMDB_ID.Text;
            string originalName = this.tbOriginalName.Text;
            string englishName = this.tbEnglishName.Text;
            string chineseName = this.tbChineseName.Text;
            string hongkongName = this.tbHongkongName.Text;
            string postLink = this.iPostLink.ImageUrl;
            string produceYear = this.tbProduceYear.Text;
            string director = this.tbDirector.Text;
            string leadingRule = this.tbLeadingRule.Text;
            string introduce = this.tbIntroduce.Text;
            string awards = this.tbAwards.Text;
            string userID = this.CurrentUser.ID;

            BLL.Movie bll = new BLL.Movie();
            if (string.IsNullOrEmpty(this.MovieID))
            {
                this.MovieID = bll.Add(iMDB_ID, originalName, englishName, chineseName, hongkongName, postLink, produceYear, director, leadingRule, introduce, awards, userID);
            }
            else
            {
                bll.Update(this.MovieID, iMDB_ID, originalName, englishName, chineseName, hongkongName, postLink, produceYear, director, leadingRule, introduce, awards, userID);
            }

            //new Nation().DeleteByMovieID(this.MovieID);
            foreach (ListItem item in cblNation.Items)
            {
                if (item.Selected == true)
                {
                    NationInfo nInfo = new BLL.Nation().GetByValue(item.Value);
                    new BLL.Movie().AddNation(this.MovieID, nInfo.ID);
                }
            }
            ShowMsg("影片信息已成功保存。");
        }
        catch (Exception ex)
        {
            LogEntry.Log.Write(ex.ToString(), EventLogEntryType.Error, LogSourceType.一般错误);
            ShowMsg(ex.Message);
        }
    }

    protected void btnImport_Click(object sender, EventArgs e)
    {
        string imdbNo = tbIMDB_ID.Text;
        MovieInfo mInfo = new BLL.Movie().GetByIMDB_NO(imdbNo);
        tbAwards.Text = mInfo.Awards;
        tbChineseName.Text = mInfo.ChineseName;
        iPostLink.ImageUrl = mInfo.PostLink;
        tbDirector.Text = mInfo.Director;
        tbEnglishName.Text = mInfo.EnglishName;
        tbHongkongName.Text = mInfo.HongkongName;
        tbIMDB_ID.Text = mInfo.IMDB_ID;
        tbIntroduce.Text = mInfo.Introduce;
        tbLeadingRule.Text = mInfo.LeadingRule;
        tbOriginalName.Text = mInfo.OriginalName;
        iPostLink.ImageUrl = mInfo.PostLink;
        tbProduceYear.Text = mInfo.ProduceYear;
        IList<NationInfo> nList = new BLL.Nation().GetByNameList(mInfo.NationListString);
        foreach (NationInfo nInfo in nList)
        {
            cblNation.Items.FindByValue(nInfo.EnglishName).Selected = true;
        }
    }

    #endregion

    #region 私有函数
    private void ShowInfo(string ID)
    {
        IList<NationInfo> nList = new BLL.Nation().GetList();
        cblNation.DataSource = nList;
        cblNation.DataBind();

        if (string.IsNullOrEmpty(ID))
        {
            return;
        }
        BLL.Movie bll = new BLL.Movie();
        MovieInfo model = bll.GetByID(ID);
        this.tbIMDB_ID.Text = model.IMDB_ID;
        this.tbOriginalName.Text = model.OriginalName;
        this.tbEnglishName.Text = model.EnglishName;
        this.tbChineseName.Text = model.ChineseName;
        this.tbHongkongName.Text = model.HongkongName;
        this.iPostLink.ImageUrl = model.PostLink;
        this.tbProduceYear.Text = model.ProduceYear;
        this.tbDirector.Text = model.Director;
        this.tbLeadingRule.Text = model.LeadingRule;
        this.tbIntroduce.Text = model.Introduce;
        this.tbAwards.Text = model.Awards;
        this.tbUser.Text = this.CurrentUser.ID;
        foreach (NationInfo nInfo in model.NationList)
        {
            cblNation.Items.FindByValue(nInfo.EnglishName).Selected = true;
        }
        // 显示下载链接
        int pageSize = 100;
        int pageIndex = 0;
        IList<DownloadLinkInfo> dList = new BLL.DownloadLink().GetList(this.MovieID, pageSize, pageIndex);
        gvUri.DataSource = dList;
        gvUri.DataBind();
    }

    #endregion

    protected void gvUri_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            string ID = e.CommandArgument.ToString();
            if (e.CommandName == "btnDel")
            {
                new BLL.User().Delete(ID);
            }
            else
            {
                #region 控件
                int index = Convert.ToInt32(ID);
                GridViewRow gvr = (GridViewRow)gvUri.Rows[index];
                TextBox tbName = (TextBox)gvr.FindControl("tbName");
                TextBox tbPwd = (TextBox)gvr.FindControl("tbPwd");
                TextBox tbTime = (TextBox)gvr.FindControl("tbTime");
                #endregion

                if (e.CommandName == "btnEdit")
                {
                    //new BLL.User().Update(tbName.Text, tbPwd.Text);
                }

                if (e.CommandName == "btnAdd")
                {
                    //new BLL.User().Add(tbName.Text, tbPwd.Text);
                }
            }
            ShowMsg("操作成功！");
            //BindList();
        }
        catch (Exception exc)
        {
            ShowMsg(exc.Message);
            LogEntry.Log.Write(exc.ToString(), EventLogEntryType.Error, LogSourceType.其他);
        }

    }
}
